using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace GameOverlayWPF
{
    /// <summary>
    /// 瀑布流布局面板 - 等宽不等高，错落有致
    /// </summary>
    public class WaterfallPanel : Panel
    {
        // 列数
        public static readonly DependencyProperty ColumnsProperty =
            DependencyProperty.Register(
                "Columns",
                typeof(int),
                typeof(WaterfallPanel),
                new FrameworkPropertyMetadata(2, FrameworkPropertyMetadataOptions.AffectsMeasure));

        public int Columns
        {
            get { return (int)GetValue(ColumnsProperty); }
            set { SetValue(ColumnsProperty, value); }
        }

        // 列间距
        public static readonly DependencyProperty ColumnSpacingProperty =
            DependencyProperty.Register(
                "ColumnSpacing",
                typeof(double),
                typeof(WaterfallPanel),
                new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsMeasure));

        public double ColumnSpacing
        {
            get { return (double)GetValue(ColumnSpacingProperty); }
            set { SetValue(ColumnSpacingProperty, value); }
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            if (Children.Count == 0 || Columns <= 0)
                return new Size(0, 0);

            // 计算每列的宽度
            double totalSpacing = ColumnSpacing * (Columns - 1);
            double columnWidth = (availableSize.Width - totalSpacing) / Columns;
            
            if (double.IsInfinity(columnWidth) || columnWidth <= 0)
                columnWidth = 200; // 默认宽度

            // 记录每列的当前高度
            double[] columnHeights = new double[Columns];

            // 测量每个子元素
            foreach (UIElement child in Children)
            {
                // 为子元素提供列宽作为约束
                child.Measure(new Size(columnWidth, double.PositiveInfinity));

                // 找到最短的列
                int shortestColumn = 0;
                for (int i = 1; i < Columns; i++)
                {
                    if (columnHeights[i] < columnHeights[shortestColumn])
                        shortestColumn = i;
                }

                // 将子元素添加到最短列的高度
                columnHeights[shortestColumn] += child.DesiredSize.Height;
            }

            // 面板的总高度是最高列的高度
            double maxHeight = columnHeights.Max();
            return new Size(availableSize.Width, maxHeight);
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            if (Children.Count == 0 || Columns <= 0)
                return finalSize;

            // 计算每列的宽度
            double totalSpacing = ColumnSpacing * (Columns - 1);
            double columnWidth = (finalSize.Width - totalSpacing) / Columns;

            // 记录每列的当前高度
            double[] columnHeights = new double[Columns];

            // 排列每个子元素
            foreach (UIElement child in Children)
            {
                // 找到最短的列
                int shortestColumn = 0;
                for (int i = 1; i < Columns; i++)
                {
                    if (columnHeights[i] < columnHeights[shortestColumn])
                        shortestColumn = i;
                }

                // 计算子元素的位置
                double x = shortestColumn * (columnWidth + ColumnSpacing);
                double y = columnHeights[shortestColumn];

                // 排列子元素
                child.Arrange(new Rect(x, y, columnWidth, child.DesiredSize.Height));

                // 更新列高度
                columnHeights[shortestColumn] += child.DesiredSize.Height;
            }

            return finalSize;
        }
    }
}
